package com.ruoyi.project.system.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.UserNotificationMapper;
import com.ruoyi.project.system.domain.UserNotification;
import com.ruoyi.project.system.service.IUserNotificationService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@Service
public class UserNotificationServiceImpl implements IUserNotificationService {
    @Autowired
    private UserNotificationMapper userNotificationMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public UserNotification selectUserNotificationById(Long id) {
        return userNotificationMapper.selectUserNotificationById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param userNotification 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<UserNotification> selectUserNotificationList(UserNotification userNotification) {
        return userNotificationMapper.selectUserNotificationList(userNotification);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param userNotification 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertUserNotification(UserNotification userNotification) {
        return userNotificationMapper.insertUserNotification(userNotification);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param userNotification 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateUserNotification(UserNotification userNotification) {
        return userNotificationMapper.updateUserNotification(userNotification);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteUserNotificationByIds(Long[] ids) {
        return userNotificationMapper.deleteUserNotificationByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteUserNotificationById(Long id) {
        return userNotificationMapper.deleteUserNotificationById(id);
    }

    /**
     * 标记通知为已读
     *
     * @param userId         用户ID
     * @param notificationId 通知ID
     * @return 是否标记成功
     */
    @Override
    public boolean markAsRead(Long userId, Long notificationId) {
        // 查询用户通知关联
        UserNotification query = new UserNotification();
        query.setUserId(userId);
        query.setNotificationId(notificationId);
        List<UserNotification> userNotifications = userNotificationMapper.selectUserNotificationList(query);

        if (userNotifications.isEmpty()) {
            // 如果不存在关联记录，创建一个新的已读记录
            UserNotification newRecord = new UserNotification();
            newRecord.setUserId(userId);
            newRecord.setNotificationId(notificationId);
            newRecord.setIsRead(1L); // 已读
            newRecord.setReadTime(new Date());
            return userNotificationMapper.insertUserNotification(newRecord) > 0;
        } else {
            // 更新为已读
            UserNotification userNotification = userNotifications.get(0);

            // 如果已经是已读状态，直接返回成功
            if (userNotification.getIsRead() != null && userNotification.getIsRead() == 1L) {
                return true;
            }

            userNotification.setIsRead(1L);
            userNotification.setReadTime(new Date());
            return userNotificationMapper.updateUserNotification(userNotification) > 0;
        }
    }

    /**
     * 标记所有通知为已读
     *
     * @param userId 用户ID
     * @return 标记的通知数量
     */
    @Override
    public int markAllAsRead(Long userId) {
        // 查询用户所有未读通知
        UserNotification query = new UserNotification();
        query.setUserId(userId);
        query.setIsRead(0L); // 未读状态
        List<UserNotification> unreadNotifications = userNotificationMapper.selectUserNotificationList(query);

        int count = 0;
        Date now = new Date();

        for (UserNotification notification : unreadNotifications) {
            notification.setIsRead(1L); // 设置为已读
            notification.setReadTime(now);
            if (userNotificationMapper.updateUserNotification(notification) > 0) {
                count++;
            }
        }

        return count;
    }
}
